package com.tianbo.controller;

import com.tianbo.common.ModelAndPage;
import com.tianbo.model.TOlapColumn;
import com.tianbo.model.TOlapDataset;
import com.tianbo.service.OlapColumnService;
import com.tianbo.service.OlapDataSetService;
import com.tianbo.util.dao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Mr.Xu on 2017/9/13.
 */
@Controller
@RequestMapping("/olapcolumn")
public class OlapColumnController {
    @Autowired
    private OlapColumnService olapColumnService;
    @Autowired
    private OlapDataSetService olapDataSetService;
    @RequestMapping("showlist")
    public ModelAndView findOlapColumn(@ModelAttribute Page page) throws Exception{
        int pagelenth=10;
        int start=0;
        if(page.getBegin()>=0){start=page.getBegin();}
        if(page.getLength()>0){pagelenth=page.getLength();}
        ModelAndView modelAndView=new ModelAndView();
        ModelAndPage modelAndPage=new ModelAndPage();
        modelAndPage=olapColumnService.findOlapColumn(start,pagelenth);
        List<TOlapDataset> olapDatasetList=olapDataSetService.findOlapDataSet();

        modelAndView.addObject("modelAndPage",modelAndPage);
        modelAndView.addObject("olapDatasetList",olapDatasetList);
        modelAndView.setViewName("olapcolumn/list");
        return modelAndView;
    }
    @RequestMapping("/selectlist")
    public ModelAndView selectOlapColumn(HttpServletRequest request,BigDecimal datasetid)throws Exception{
        String id=request.getParameter("datasetid");
        BigDecimal bd=new BigDecimal(id);
        ModelAndView modelAndView=new ModelAndView();
        List<TOlapColumn> tOlapColumnList=olapColumnService.selectOlapColumn(bd);
        TOlapDataset tOlapDataset=olapDataSetService.findByid(bd);
        modelAndView.addObject("tOlapColumnList",tOlapColumnList);
        modelAndView.addObject("tOlapDataset",tOlapDataset);
        modelAndView.setViewName("olapcolumn/list");
        return modelAndView;
    }
    @RequestMapping("/findByid")
    public ModelAndView findByid(HttpServletRequest request,BigDecimal columnid) throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String id=request.getParameter("columnid");
        BigDecimal bd=new BigDecimal(id);
        TOlapColumn olapColumn=olapColumnService.findByid(bd);
        List<TOlapDataset> olapDatasetList=olapDataSetService.findOlapDataSet();
        modelAndView.addObject("olapDatasetList",olapDatasetList);
        modelAndView.addObject("olapColumn",olapColumn);
        modelAndView.setViewName("olapcolumn/edit");
        return modelAndView;
    }
    @RequestMapping("/addColumn")
    public String addOlapColumn(HttpServletRequest request,TOlapColumn olapColumn,RedirectAttributes attr){
        String columnname=request.getParameter("columnname");
        String datafield=request.getParameter("datafield");
        String nodename=request.getParameter("nodename");
        String active=request.getParameter("active");
        String datasetid=request.getParameter("datasetid");
        olapColumn.setActive(active);
        olapColumn.setDatasetid(new BigDecimal(datasetid));
        olapColumn.setNodename(nodename);
        olapColumn.setColumnname(columnname);
        olapColumn.setDatafield(datafield);
        olapColumnService.addOlapColumn(olapColumn);
        attr.addAttribute("datasetid",new BigDecimal(datasetid));
        return "redirect:/olapcolumn/selectlist";
    }
    @RequestMapping("/deleteByPrimaryKey")
    public String deleteByPrimaryKey(HttpServletRequest request,BigDecimal columnid,RedirectAttributes attr){
        String id=request.getParameter("columnid");
        String datasetid=request.getParameter("datasetid");
        BigDecimal bd=new BigDecimal(id);
        olapColumnService.deleteByPrimaryKey(bd);
        attr.addAttribute("datasetid",new BigDecimal(datasetid));
        return "redirect:/olapcolumn/selectlist";
    }
    @RequestMapping("/updateColumn")
    public String updateColumn(HttpServletRequest request,TOlapColumn olapColumn,RedirectAttributes attr){
        String id=request.getParameter("columnid");
        BigDecimal bd=new BigDecimal(id);
        String columnname=request.getParameter("columnname");
        String datafield=request.getParameter("datafield");
        String nodename=request.getParameter("nodename");
        String datasetid=request.getParameter("datasetid");
        String active=request.getParameter("active");
        olapColumn.setColumnid(bd);
        olapColumn.setColumnname(columnname);
        olapColumn.setDatafield(datafield);
        olapColumn.setNodename(nodename);
        olapColumn.setDatasetid(new BigDecimal(datasetid));
        olapColumn.setActive(active);
        olapColumnService.updateOlapColumn(olapColumn);
        attr.addAttribute("datasetid",new BigDecimal(datasetid));
        return "redirect:/olapcolumn/selectlist";
    }
    @RequestMapping("list")
    public String list(){return "olapcolumn/list";}

}
